package com.cn.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.cn.dao.ProductCategoryDao;
import com.cn.domain.Category;
import com.cn.domain.Product;
import com.cn.utils.DBUtils;

public class ProductCategoryDaoImpl implements ProductCategoryDao{

	//商品分类：获取所有的父节点
	@Override
	public List<Category> queryAllParent(){
		List<Category> list=new ArrayList();
		try {
			list=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT_CATEGORY where HPC_ID=HPC_PARENT_ID",new BeanListHandler<Category>(Category.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	//商品分类：获取所有的子节点
	@Override
	public List<Category> queryAllChild(){
		List<Category> list=new ArrayList();
		try {
			list=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT_CATEGORY where HPC_ID!=HPC_PARENT_ID",new BeanListHandler<Category>(Category.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//获取产品信息
	@Override
	public List<Product> queryProductById(String type, String id,int pageNow,int pageSize) {
		//System.out.println("dao的type="+type+"==Pagenow="+pageNow);
		//System.out.println("到的id="+id);
		List<Product> list=new ArrayList();
		try {
			int num=(pageNow-1)*pageSize;
			int end=pageNow*pageSize; 
			String sql="select * from (select t1.*,rownum rn from (select * from HWUA_PRODUCT where "+type+"=?) t1 where rownum<="+end+") where rn>"+num+"";
			Object[] params= {id};
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Product>(Product.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public List<Product> queryAllProductById(String type, String id) {
		System.out.println("dao的总数的type="+type);
		System.out.println("到的总数的id="+id);
		List<Product> list=new ArrayList();
		try {
			Object[] params= {id};
			list=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT where "+type+"=?",new BeanListHandler<Product>(Product.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		System.out.println("dao的list="+list);
		return list;
	}
	@Override
	public Product queryProById(String id) {
		Product pro=null;
		try {
			Object[] params={id};
			pro=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT where HP_ID=?",new BeanHandler<Product>(Product.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return pro;
	}
	
	//更新PID更新库存量，也就是减少库存数量
	public int updateStockByProId(long num,long id){
		int i=0;
		try {
			Object[] params= {num,id};
			i=DBUtils.getQuryRunner().update("update HWUA_PRODUCT set HP_STOCK=? where HP_ID=?",params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return i;
	}
	
	//获取产品信息
	@Override
	public List<Product> queryProByName(String type, String name,int pageNow,int pageSize) {
		List<Product> list=new ArrayList();
		try {
			int num=(pageNow-1)*pageSize;
			int end=pageNow*pageSize; 
			String sql="select * from (select t1.*,rownum rn from (select * from HWUA_PRODUCT where HP_NAME like '%"+name+"%') t1 where rownum<="+end+") where rn>"+num+"";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	//获取产品信息
	@Override
	public List<Product> queryProCount(String name) {
		List<Product> list=new ArrayList();
		try {
			String sql="select * from HWUA_PRODUCT where hp_name like '%"+name+"%'";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	//
	@Override
	public List<Product> queryProByName(String name) {
		List<Product> list=new ArrayList();
		try {
			String sql="select * from  HWUA_PRODUCT where HP_NAME like '%"+name+"%'";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	
	//获取所有的产品信息
	@Override
	public List<Product> queryAllProduct() {
		List<Product> list=new ArrayList();
		try {
			String sql="select * from  HWUA_PRODUCT";
			list=DBUtils.getQuryRunner().query(sql,new BeanListHandler<Product>(Product.class));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public Category queryCateById(String type,long id){
		Category cate=null;
		try {
			Object[] params={id};
			cate=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT_CATEGORY where "+type+"=?",new BeanHandler<Category>(Category.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cate;
	}
	
	
	
	//根据ID号差商品
	public Product queryProById(String type,long id,long pid){
		Product cate=null;
		try {
			Object[] params={id,pid};
			cate=DBUtils.getQuryRunner().query("select * from HWUA_PRODUCT where "+type+"=? and HP_ID=?",new BeanHandler<Product>(Product.class),params);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return cate;
	}
	
}
